package com.googlecode.icecube.presentation;

import java.util.ArrayList;
import java.util.List;
import java.util.LinkedHashMap;

import com.googlecode.icefusion.ui.commons.BackingBeanForm;
import com.googlecode.icefusion.ui.commons.form.ITable;
import com.googlecode.icefusion.ui.commons.form.ITableRowSortable;

/**
 * Manages data table examples.
 * 
 * @author Rainer Eschen
 *
 */
public class DataTable extends BackingBeanForm implements ITable {

	private static final long serialVersionUID = -339904117919557582L;

	/**
	 * Some rows to show in the table
	 */
	private ArrayList<DataTableRow4JavaAuthors> authors = new ArrayList<DataTableRow4JavaAuthors>();
	
	/**
	 * All attributes of the row object to use in the table.
	 */
	private LinkedHashMap<String,String> attributes = new LinkedHashMap<String,String>();
	
	/**
	 * true: everything ready to start
	 */
	private Boolean initialized = false;
	
	public ArrayList getAuthors() {
		return authors;
	}

	public void setAuthors(ArrayList authors) {
		this.authors = authors;
	}

	public LinkedHashMap<String, String> getAttributes() {
		return attributes;
	}

	public void setAttributes(LinkedHashMap<String, String> attributes) {
		this.attributes = attributes;
	}

	protected void init() {
		
		authors.clear();
		authors.add(new DataTableRow4JavaAuthors("Rod","Johnson","http://blog.springsource.com/author/rodj/",false));
		authors.add(new DataTableRow4JavaAuthors("Matt","Raible","http://raibledesigns.com/",false));
		authors.add(new DataTableRow4JavaAuthors("Ted","Goddard","http://blog.icefaces.org/blojsom/blog/default/Ted%20Goddard/",true));
		authors.add(new DataTableRow4JavaAuthors("Matthias","Wessendorf","http://matthiaswessendorf.wordpress.com/",true));
		authors.add(new DataTableRow4JavaAuthors("Christophe","Coenraets","http://coenraets.org/",false));
		authors.add(new DataTableRow4JavaAuthors("Rick","Hightower","http://www.jroller.com/RickHigh/",true));
		
		// We define the order of attribute-to-column presentation
		// first is attribute name (id match), second is resource bundle id (textual presentation)
		attributes.clear();
		attributes.put("lastname","application.menu.presentation.dataTable.dynamic.attribute.lastname");
		attributes.put("firstname","application.menu.presentation.dataTable.dynamic.attribute.firstname");
		attributes.put("jsfFocus","application.menu.presentation.dataTable.dynamic.attribute.jsfFocus");
		attributes.put("homepage","application.menu.presentation.dataTable.dynamic.attribute.homepage");
		this.initialized = true;
	}

	public LinkedHashMap<String, String> getColumnsMap() {
		
		if (!this.initialized) {
			init();
		}
		return this.getAttributes();
	}

	public List<ITableRowSortable> getRowsList() {
		
		if (!this.initialized) {
			init();
		}
		return this.getAuthors();
	}
}
